#include "common_query_checker.h"
#include "../../util.h"

CommonQuery::CommonQuery(iostream *input) : EdgeChecker("CommonQuery")
{
	string line;
	while(!input->eof())
	{
		getline(*input, line);
		if(line.length() == 0)continue;
		int del = line.find(':');
		string query = line.substr(0, del);
		string elem = line.substr(del + 1, line.length() - del - 1);
		vector<string> poss = util::tokenize_h(elem, " | ");
		set<string> s;
		for(int i = 0; i < poss.size(); ++ i)
			s.insert(poss[i]);
		dic.insert(pair<string, set<string>>(query, s));
	}
}
		
bool CommonQuery::satisfied(QueryNode &n1, QueryNode &n2)
{
	stringstream ss;
	ss << "#" << n1.name << "======" << n2.name << "#" << endl;
	util::LogMgr::record(ss.str());
	set<string> &s1 = (*dic.find(n1.name)).second;
	set<string> &s2 = (*dic.find(n2.name)).second;
	set<string>::iterator itr;
	for(itr = s1.begin(); itr != s1.end(); ++ itr)
		if(s2.find(*itr) != s2.end())
			return true;
	return false;
}